package ModLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class SaleCon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleCon
{
    public static SaleCon instance;
    private ArrayList<Sale> saleList;
    private ProductCon productcon;

    private SaleCon()
    {
        saleList = new ArrayList<Sale>();
        productcon = productcon.getInstance();
    }

    public static SaleCon getInstance()
    {
        if(instance==null)
            instance = new SaleCon();
        return instance;
    }

    public void createSale(String id, String IDEmployee, String IDCustomer, String creationDate)
    {
        Sale sale = new Sale(id, IDEmployee, IDCustomer, creationDate);
        saleList.add(sale);
    }

    public void removeSale(String id)
    {
        saleList.remove(findSale(id));
    }

    public Sale findSale(String id)
    {
        Sale returnsale = null;
        for(Sale x : saleList)
        {
            if(id.equals(x.getID()))
            {
                returnsale = x;
            }
        }
        return returnsale;
    }

    public void addProduct(String id, String p, int qua)
    {
        for(Sale x : saleList)
        {
            if(id.equals(x.getID()))
            {
                x.addProduct(p, qua);
            }
        }
    }

    public void removeProduct(String id, String p)
    {
        Iterator<Sale> it = saleList.iterator();
        while(it.hasNext())
        {
            Sale sale = it.next();
            if(sale.getID().equals(id))
            {
                it.remove();
            }
        }
    }

    public int deleteOutDatedWishlist(String date)
    {
        int counter = 0;
        Iterator<Sale> it = saleList.iterator();
        while(it.hasNext())
        {
            Sale s = it.next();
            if(s.getCreationDate().equals(date))
            {
                it.remove();
                counter++;
            }
        }
        return counter;
    }
    
    public ArrayList<Sale> getSaleList()
    {
        return saleList;
    }
}